package com.design.classroommanagement

import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.cf.annotation.Holder
import com.cf.holder.BaseHolder
import com.cf.holder.QuickAdapter
import com.design.classroommanagement.api.*
import com.design.classroommanagement.api.dao.BookmarkDao
import com.design.classroommanagement.manager.UserAccountManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.laughing.lib.base.HBaseListViewModel
import com.laughing.lib.base.bindHolder
import com.laughing.lib.base.getVm
import com.laughing.lib.ui.selector
import com.laughing.lib.utils.*
import kotlinx.android.synthetic.main.item_add_bookmark.view.*
import kotlinx.android.synthetic.main.item_bookmark.view.*
import kotlinx.android.synthetic.main.item_user.view.nameTv

@Route(path = Pages.BookmarkListFragment)
class BookmarkListFragment : BaseListFragment<BookmarkListViewModel>() {
    override fun bindHolder(adapter: QuickAdapter) {
        adapter.bindHolder<BookmarkHolder>()
        adapter.bindHolder<AddBookmarkHolder>()
        binding.root.setBackgroundColor("#eeeeee".parseColor())
        setLoadMoreEnable(false)
    }

    override fun initCompleted() {
        super.initCompleted()

        binding.nav.titleTv.text = "课程列表"

        viewModel.updateLiveData.observe(this) {
            onRefresh()
        }
    }

    override fun onResume() {
        super.onResume()
        onRefresh()
    }

}

class BookmarkListViewModel : HBaseListViewModel() {
    val updateLiveData = LiveEventBus.get<Boolean>(javaClass.canonicalName!!)

    override fun loadData(result: (List<*>) -> Unit, exception: (Exception) -> Unit) {
        load {
            val userList: MutableList<Any> = mutableListOf()

            val user = UserAccountManager.user
            when (user?.role) {
                Role.admin -> {
                    userList.addAll(BookmarkDao.query())
                    userList.add(AddBookmarkData())
                }
                Role.teacher -> {
                    userList.addAll(BookmarkDao.query() {
                        put("tid", user.id ?: "")
                    })
                }
                Role.student -> {
                    userList.addAll(BookmarkDao.query())
                }
                null -> TODO()
            }

            kotlinx.coroutines.delay(500)
            userList
        }.result({
            it?.let { it1 -> result(it1) }
        })
    }
}


@Holder
class AddBookmarkHolder @JvmOverloads constructor(
    parent: ViewGroup?,
    layoutId: Int = R.layout.item_add_bookmark
) : BaseHolder<AddBookmarkData>(parent, layoutId) {
    override fun convert(data: AddBookmarkData) {
        itemView.addBtn.clickWithTrigger {
            openPage(Pages.AddCourseFragment)
        }
    }
}

class AddBookmarkData


@Holder
class BookmarkHolder @JvmOverloads constructor(
    parent: ViewGroup?,
    layoutId: Int = R.layout.item_bookmark
) :
    BaseHolder<Bookmark>(parent, layoutId) {
    override fun convert(data: Bookmark) {
        itemView.apply {
            nameTv.text = data.name
            teacherTv.text = data.tid
            classroomTv.text = data.classname
            dateTv.text = data.date

            clickWithTrigger {
                openPage(Pages.AddCourseFragment) {
                    withSerializable("data", data)
                }
            }
            if (UserAccountManager.user?.role == Role.admin) {
                setOnLongClickListener {
                    topActivity.selector(mutableListOf("删除"), "操作") { _, index ->
                        when (index) {
                            0 -> {
                                runInBg {
                                    BookmarkDao.delete(data)
                                    getVm<BookmarkListViewModel>()?.updateLiveData?.post(true)
                                }
                            }
                            else -> {}
                        }
                    }
                    true
                }
            }
        }
    }
}

